package net.fltx.assets.config;


import com.google.gson.Gson;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Component
public class MimeType {


    private HashMap<String,String> mimeTypeMap;

    @PostConstruct
    public void init() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("mime.json");
        //System.out.println(classPathResource.exists());
        InputStreamReader reader=new InputStreamReader(classPathResource.getInputStream());
        Gson gson=new Gson();
        mimeTypeMap=gson.fromJson(reader,HashMap.class);
    }

    public String get(String fileName){

        int p=fileName.lastIndexOf('.');
        if(p>=0 && p<fileName.length()){
            String ext=fileName.toLowerCase().substring(p+1);
            if(ext.length()>0){
                ext=mimeTypeMap.get(ext);
                if(ext!=null) return ext;
            }
        }

        return mimeTypeMap.get("*");
    }

}
